﻿Imports System.Windows.Forms

Public Class ChangeRequestDialog

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click


        If AllocationBox.Text <> 0 Then
            If Trim(NotesBox.Text) = "" Then

                MessageBox.Show("Please enter a note", "Enter note", MessageBoxButtons.OK, MessageBoxIcon.Error)

                Exit Sub

            End If
        Else
            If MessageBox.Show("This will make the current allocation 0, are you sure you want to continue?", "Zero allocation?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
                Exit Sub

            End If
        End If

        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub AllocationBox_Validated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AllocationBox.Validated

        Me.AllocationBox.Text = StringToCurrency(Me.AllocationBox.Text)

    End Sub



    Function ValidateBox(ByVal Box As TextBox) As Boolean
        Dim currency As Decimal

        ValidateBox = False

        'Convert the current value to currency, with or without a currency symbol.
        If Not Decimal.TryParse(Box.Text, _
                                Globalization.NumberStyles.Currency, _
                                Nothing, _
                                currency) Then
            'Don't let the user leave the field if the value is invalid.
            With Box
                .HideSelection = False
                .SelectAll()

                MessageBox.Show("Please enter a valid currency amount.", _
                                "Invalid Value", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error)

                .HideSelection = True
            End With

            ValidateBox = True
        End If

    End Function

    Private Sub AllocationBox_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles AllocationBox.Validating
        e.Cancel = ValidateBox(Me.AllocationBox)

    End Sub

    Private Sub RateBox_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles RateBox.Validating
        e.Cancel = ValidateBox(Me.RateBox)

    End Sub

    Private Sub RateBox_Validated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RateBox.Validated
        Me.RateBox.Text = StringToCurrency(Me.RateBox.Text)

    End Sub

    Private Sub PFBox_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles PFBox.Validating
        Dim test As Decimal

        If Not Decimal.TryParse(Me.PFBox.Text, test) Then
            With Me.PFBox
                .HideSelection = False
                .SelectAll()

                MessageBox.Show("Please enter a valid amount.", _
                                "Invalid Value", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error)

                .HideSelection = True
            End With
        End If
    End Sub


    Private Sub LearnerBox_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles LearnerBox.Validating
        Dim test As Integer

        If Not Integer.TryParse(Me.LearnerBox.Text, test) Then
            With Me.LearnerBox
                .HideSelection = False
                .SelectAll()

                MessageBox.Show("Please enter a valid amount.", _
                                "Invalid Value", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error)

                .HideSelection = True
            End With
        End If
    End Sub
End Class
